import { createStore, applyMiddleware, compose } from 'redux'
import rootReducer from '../rootReducer'
// import DevTools from '../containers/DevTools'
import { routerMiddleware } from 'react-router-redux'
import { browserHistory } from 'react-router'
import createSagaMiddleware from 'redux-saga'

import appSaga from '../containers/App/sagas'
import postSaga from '../containers/PostList/sagas'
import gamePostListSaga from '../containers/GamePostList/sagas'
import gamePostEditorSaga from '../containers/GamePostEditor/sagas'
import gamePostDisplaySaga from '../containers/GamePostDisplay/sagas'
import commentListSaga from '../containers/CommentList/sagas'
import commentEditorSaga from '../containers/CommentEditor/sagas'

export default function configureStore(initialState) {

  const sagaMiddleware = createSagaMiddleware()
 
  const router_middleware = routerMiddleware(browserHistory)

  const store = createStore(
    rootReducer,
    initialState,
    compose(
      applyMiddleware(router_middleware, sagaMiddleware),
      // DevTools.instrument()
    )
  )

  sagaMiddleware.run(appSaga)
  sagaMiddleware.run(postSaga)
  sagaMiddleware.run(gamePostListSaga)
  sagaMiddleware.run(gamePostEditorSaga)
  sagaMiddleware.run(gamePostDisplaySaga)
  sagaMiddleware.run(commentListSaga)
  sagaMiddleware.run(commentEditorSaga)


  // if (module.hot) {
  //   // Enable Webpack hot module replacement for reducers
  //   module.hot.accept('../rootReducer', () => {
  //     const nextRootReducer = require('../rootReducer').default
  //     store.replaceReducer(nextRootReducer)
  //   })
  // }

  return store
}